"""
 encoding    : utf-8
 @File       : view_survey_page.py
 @Author     : Rao
 @Version    :
 @Description: 查看调查页面
 @Date       : 2024/06/20
"""
import time

import pytest
from selenium.common import StaleElementReferenceException, NoSuchElementException, TimeoutException

from pages.basepage import *


class ViewSurveyPage(BasePage):
    """查看调查页面的页面元素"""

    def find_survey_title(self):
        """调查标题"""
        return WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(
                (
                    By.XPATH,
                    '/html/body/form[1]/div[2]/div/div[1]/div[2]/table/tbody/tr[1]/td[2]/div/a'
                )
            )
        )

    def find_check_number(self):
        """点击次数"""
        return self.driver.find_element(
            By.XPATH,
            "/html/body/form[1]/div[2]/div/div[1]/div[2]/table/tbody/tr[1]/td[7]/div"
        )

    def switch_to_survey_frame(self):
        """切换到调查页面的frame"""
        self.back_frame_root()
        survey_frame = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(
                (
                    By.CSS_SELECTOR,
                    "[style='margin:0px;width:960px;height:620px']"
                )
            )
        )
        self.driver.switch_to.frame(survey_frame)

    def find_check(self):
        """选项按钮"""
        return WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(
                (
                    By.ID,
                    "option_0_0"
                )
            )
        )

    def find_other_check(self):
        """其他选项按钮"""
        return WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(
                (
                    By.ID,
                    "otheroption_0"
                )
            )
        )

    def find_other_check_input(self):
        """其他选项输入框"""
        return self.driver.find_element(
            By.ID,
            "input_0"
        )

    def find_submit_button(self):
        """提交按钮"""
        return self.driver.find_element(
            By.ID,
            "button_submit"
        )

    def find_close_button(self):
        """关闭弹窗"""
        return self.driver.find_element(
            By.XPATH,
            "/html/body/div[1]/div/table/tbody/tr[2]/td[2]/div/table/tbody/tr[1]/td/div/a"
        )

    def find_del_button(self):
        """删除按钮"""
        return self.driver.find_element(
            By.XPATH,
            "/html/body/form[1]/div[2]/div/div[1]/div[2]/table/tbody/tr[1]/td[8]/div/a[1]"
        )

    def find_stop_button(self):
        """停止按钮"""
        return self.driver.find_element(
            By.XPATH,
            "/html/body/form[1]/div[2]/div/div[1]/div[2]/table/tbody/tr[1]/td[8]/div/a[2]"
        )

    def find_msg(self):
        """查找提示信息"""
        return WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(
                (
                    By.CSS_SELECTOR,
                    "[style='max-width:440px;']"
                )
            )
        )

    def find_msg_yes_button(self):
        """确认按钮"""
        return self.driver.find_element(
            By.CSS_SELECTOR,
            "input.d-button:nth-child(1)"
        )

    def find_msg_no_button(self):
        """取消按钮"""
        return self.driver.find_element(
            By.CSS_SELECTOR,
            "input.d-button:nth-child(2)"
        )


class ViewSurveyAction:
    """执行页面操作"""

    def __init__(self, page, driver):
        self.page = page
        self.driver = driver

    def click_title(self):
        """点击调查标题"""
        # 进入main_frame
        self.page.switch_to_main_frame()
        # 点击标题
        self.page.find_survey_title().click()

    def click_check(self):
        """点击选项按钮"""
        self.page.switch_to_survey_frame()
        self.page.find_check().click()

    def click_other_check(self):
        """点击其他"""
        self.page.switch_to_survey_frame()
        self.page.find_other_check().click()

    def input_other_check(self, content):
        """其他-文档输入"""
        self.page.find_other_check_input().send_keys(content)

    def check_button_text(self):
        """检查按钮状态"""
        self.page.switch_to_survey_frame()
        return self.page.find_submit_button().get_attribute("title")

    def check_number(self):
        """获取点击次数"""
        self.page.switch_to_main_frame()
        return self.page.find_check_number().text

    def click_close_button(self):
        """点击关闭按钮"""
        self.page.back_frame_root()
        self.page.find_close_button().click()

    def click_vote(self):
        """点击确认投票"""
        self.page.switch_to_survey_frame()
        self.page.find_submit_button().click()

    def click_delete(self):
        """点击删除按钮"""
        self.page.switch_to_main_frame()
        self.page.find_del_button().click()

    def second_confirmation(self):
        """二次确认"""
        self.page.back_frame_root()
        self.page.find_msg_yes_button().click()

    def cancel_confirmation(self):
        """取消确认"""
        self.page.back_frame_root()
        self.page.find_msg_no_button().click()

    def click_stop(self):
        """点击停止按钮"""
        self.page.find_stop_button().click()

    def assert_pop_window_right(self) -> None:
        """操作成功时, 通过弹窗内容进行断言"""
        self.page.back_frame_root()
        try:
            assert self.page.find_msg().text in (
                '操作成功！',
                '投票成功！',
            ), '操作失败'
        except NoSuchElementException:
            assert pytest.skip('未找到页面元素, 跳过此次用例'), '操作失败'

    def assert_button_text(self) -> None:
        """通过按钮文字进行断言"""
        assert self.check_button_text() == '感谢您参与此调查', '操作失败，按钮文本不符合预期'

    def assert_clik_number(self, number1, number2) -> None:
        """校验点击次数是否刷新"""
        assert number1 == number2, "点击次数未刷新"

    def assert_file(self) -> None:
        """操作失败断言"""
        pass
        # self.page.back_frame_root()
        # assert self.page.find_submit_msg().text == ''
